class Admin::UsersController < ApplicationController
  before_filter :check_administrator_role,
                :only => [:index, :destroy, :enable]
  before_filter :login_required,
                :only => [:edit,:update]
  def index
    @users = User.find(:all)
    @page_title = 'Listing users'
  end
    
  def new
    @user = User.new
  end

  def create    
    @user = User.new(params[:user])
    if @user.save
      flash[:notice] = "User #{@user.username} was successfully created."
      redirect_to login_path
    else
      @page_title = 'Create new user'
      render :action => 'new'      
    end
  end

  def edit
    @user = User.find(params[:id])
    @page_title = 'Edit user'
  end


  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:notice] = "user #{@user.username} was successfully updated."
      redirect_to :action => 'index'
    else
      render :action => 'edit'
    end
  end

  def destroy
    @user = User.find(params[:id])
    if @user.update_attribute(:enabled, false)
      flash[:notice] = "User disabled"
    else
      flash[:error] = "There was a problem disabling this user."
    end
    redirect_to :action => 'index'
  end
  
  def enable
    @user = User.find(params[:id])
    if @user.update_attribute(:enabled, true)
      flash[:notice] = "User enabled"
    else
      flash[:error] = "There was a problem enabling this user."
    end
    redirect_to :action => 'index'
  end

  def show
    @user = User.find(params[:id])
    render :action => 'show'
  end



end
